package net.zoneland.knowledge.config;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
 * .
 *
 * @author: bcc
 * 2020/9/15
 */
@Configuration
@ConfigurationProperties(prefix = "primary.datasource")
@MapperScan(basePackages = MybatisConfig.PACKAGE, sqlSessionFactoryRef = "sqlSessionFactory")
public class MybatisConfig {

    /**
     * dao扫描路径
     */
    static final String PACKAGE = "net.zoneland.knowledge.dao";
    /**
     * 类型.
     */
    private String type;
    /**
     * 数据库属性配置项.
     */
    private DataSourcePropsVO druid;

    /**
     * HR数据源.
     * @return
     */
    @Bean("dataSource")
    @Primary
    public DataSource dataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druid.initProperties(druidDataSource);
        return druidDataSource;
    }

    /**
     * hr的sqlSessionFactory
     * @param dataSource
     * @return
     * @throws Exception
     */
    @Bean("sqlSessionFactory")
    public SqlSessionFactory sessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
//        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/primary/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }

    /**
     * hr的查询template.
     * @param sessionFactory
     * @return
     */
    @Bean("sqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sessionFactory) {
        return new SqlSessionTemplate(sessionFactory);
    }

    @Bean(name = "transactionManager")
    @Primary
    public DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
//
//    /**
//     * 将名为 uniflowDataSource 的数据源注入到 SqlSessionFactory
//     */
//    @Bean(name = "sqlSessionFactory")
//    @Primary
//    public SqlSessionFactory sqlSessionFactory(DataSource masterDataSource)
//            throws Exception {
//        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
//        sessionFactory.setDataSource(masterDataSource);
//        return sessionFactory.getObject();
//    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public DataSourcePropsVO getDruid() {
        return druid;
    }

    public void setDruid(DataSourcePropsVO druid) {
        this.druid = druid;
    }
}
